package best.driver.android.common.account;

import java.util.ArrayList;
import java.util.WeakHashMap;

public class AccountManager implements IAccountManager {

    private WeakHashMap<AccountManagerListener, Void> mListeners = new WeakHashMap<AccountManagerListener, Void>();

    
    @Override
	public boolean isCustomerLogin() {
		return false;
	}
    
    @Override
    public IAccount getCustomer() {
        return null;
    }

    @Override
    public void logout() {
        notifyAccountDidLogout();
    }

    @Override
    public void addListener(AccountManagerListener listener) {
        mListeners.put(listener, null);
    }

    @Override
    public void removeListener(AccountManagerListener listener) {
        mListeners.remove(listener);
    }

    protected void notifyAccountDidLogin() {
        ArrayList<AccountManagerListener> allListeners = new ArrayList<AccountManagerListener>(mListeners.keySet());
        for (AccountManagerListener aListener : allListeners) {
            aListener.onAccountDidLogin(getCustomer());
        }
    }

    protected void notifyAccountDidLogout() {
        ArrayList<AccountManagerListener> allListeners = new ArrayList<AccountManagerListener>(mListeners.keySet());
        for (AccountManagerListener aListener : allListeners) {
            aListener.onAccountDidLogout();
        }
    }

    protected void notifyAccountDidUpdate() {
        ArrayList<AccountManagerListener> allListeners = new ArrayList<AccountManagerListener>(mListeners.keySet());
        for (AccountManagerListener aListener : allListeners) {
            aListener.onAccountProfileUpdate(getCustomer());
        }
    }
    
    protected void notifyIdentyChange() {
        ArrayList<AccountManagerListener> allListeners = new ArrayList<AccountManagerListener>(mListeners.keySet());
        for (AccountManagerListener aListener : allListeners) {
            aListener.onNotifyIdentyChange();
        }
    }

    public void notifyTokenExpired() {
    	ArrayList<AccountManagerListener> allListeners = new ArrayList<AccountManagerListener>(mListeners.keySet());
    	for (AccountManagerListener aListener : allListeners) {
    		aListener.onTokenExpired();;
    	}
    }

    public void notifyCheckStatusChange() {
        ArrayList<AccountManagerListener> allListeners = new ArrayList<AccountManagerListener>(mListeners.keySet());
        for (AccountManagerListener aListener : allListeners) {
            aListener.onCheckStatusChange();
        }
    }
}
